package com.zatech.cgnci.project.base.config;

import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import com.google.common.collect.Lists;
import com.zatech.cgnci.project.base.constant.AuthHeaderConstant;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.builders.RequestParameterBuilder;
import springfox.documentation.service.ParameterType;
import springfox.documentation.service.RequestParameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.List;

/**
 * @author Cristin
 * @since 2024/1/20
 */
@Configuration
@EnableSwagger2
@EnableKnife4j
public class SwaggerConfig implements WebMvcConfigurer {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/doc.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
    }

    @Bean
    public Docket docket() {
        List<RequestParameter> parameters = Lists.newArrayList();
        RequestParameterBuilder parameterBuilder = new RequestParameterBuilder();
        RequestParameter token = parameterBuilder.name(AuthHeaderConstant.auth_request_token_header).description("token").in(ParameterType.HEADER).build();
        RequestParameter type = parameterBuilder.name(AuthHeaderConstant.auth_request_operate_type_header).description("操作日志类型(1-登录,2-菜单,3-按钮)").in(ParameterType.HEADER).build();
        RequestParameter desc = parameterBuilder.name(AuthHeaderConstant.auth_request_operate_desc_header).description("操作日志描述(例如:菜单模块:查询菜单列表)").in(ParameterType.HEADER).build();
        parameters.add(token);
        parameters.add(type);
        parameters.add(desc);

        Docket docket = new Docket(DocumentationType.SWAGGER_2)
                //.enable(true)
                .apiInfo(new ApiInfoBuilder().title("中广核保险核心系统").version("1.0.0").build())
                .select()
                //.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                .apis(RequestHandlerSelectors.basePackage("com.zatech.cgnci"))
                .paths(PathSelectors.any())
                .build();

        docket.globalRequestParameters(parameters);
        return docket;
    }

}
